home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C24 / Recycle2.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  2.8 KB  |  118 lines

  1. //: C24:Recycle2.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Chapter XX example w/ RTTI
  7. #include "../purge.h"
  8. #include <fstream>
  9. #include <vector>
  10. #include <typeinfo>
  11. #include <cstdlib>
  12. #include <ctime>
  13. using namespace std;
  14. ofstream out("recycle2.out");
  15.  
  16. class Trash {
  17.   float _weight;
  18. public:
  19.   Trash(float wt) : _weight(wt) {}
  20.   virtual float value() const = 0;
  21.   float weight() const { return _weight; }
  22.   virtual ~Trash() { out << "~Trash()\n"; }
  23. };
  24.  
  25. class Aluminum : public Trash {
  26.   static float val;
  27. public:
  28.   Aluminum(float wt) : Trash(wt) {}
  29.   float value() const { return val; }
  30.   static void value(int newval) {
  31.     val = newval;
  32.   }
  33. };
  34.  
  35. float Aluminum::val = 1.67;
  36.  
  37. class Paper : public Trash {
  38.   static float val;
  39. public:
  40.   Paper(float wt) : Trash(wt) {}
  41.   float value() const { return val; }
  42.   static void value(int newval) {
  43.     val = newval;
  44.   }
  45. };
  46.  
  47. float Paper::val = 0.10;
  48.  
  49. class Glass : public Trash {
  50.   static float val;
  51. public:
  52.   Glass(float wt) : Trash(wt) {}
  53.   float value() const { return val; }
  54.   static void value(int newval) {
  55.     val = newval;
  56.   }
  57. };
  58.  
  59. float Glass::val = 0.23;
  60.  
  61. // Sums up the value of the Trash in a bin:
  62. template<class Container> void
  63. sumValue(Container& bin, ostream& os) {
  64.   typename Container::iterator tally = 
  65.     bin.begin();
  66.   float val = 0;
  67.   while(tally != bin.end()) {
  68.     val += (*tally)->weight() * (*tally)->value();
  69.     os << "weight of "
  70.         << typeid(*tally).name()
  71.         << " = " << (*tally)->weight() << endl;
  72.     tally++;
  73.   }
  74.   os << "Total value = " << val << endl;
  75. }
  76.  
  77. int main() {
  78.   srand(time(0)); // Seed random number generator
  79.   vector<Trash*> bin;
  80.   // Fill up the Trash bin:
  81.   for(int i = 0; i < 30; i++)
  82.     switch(rand() % 3) {
  83.       case 0 :
  84.         bin.push_back(new Aluminum(rand() % 100));
  85.         break;
  86.       case 1 :
  87.         bin.push_back(new Paper(rand() % 100));
  88.         break;
  89.       case 2 :
  90.         bin.push_back(new Glass(rand() % 100));
  91.         break;
  92.     }
  93.   // Note difference w/ chapter 14: Bins hold
  94.   // exact type of object, not base type:
  95.   vector<Glass*> glassBin;
  96.   vector<Paper*> paperBin;
  97.   vector<Aluminum*> alBin;
  98.   vector<Trash*>::iterator sorter = bin.begin();
  99.   // Sort the Trash:
  100.   while(sorter != bin.end()) {
  101.     Aluminum* ap =
  102.       dynamic_cast<Aluminum*>(*sorter);
  103.     Paper* pp =
  104.       dynamic_cast<Paper*>(*sorter);
  105.     Glass* gp =
  106.       dynamic_cast<Glass*>(*sorter);
  107.     if(ap) alBin.push_back(ap);
  108.     if(pp) paperBin.push_back(pp);
  109.     if(gp) glassBin.push_back(gp);
  110.     sorter++;
  111.   }
  112.   sumValue(alBin, out);
  113.   sumValue(paperBin, out);
  114.   sumValue(glassBin, out);
  115.   sumValue(bin, out);
  116.   purge(bin);
  117. } ///:~
  118.